package com.usian.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usian.demo.mapper.UserMapper;
import com.usian.demo.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserMapper userMapper;


    @RequestMapping("add")
    @ResponseBody
    public void add(){
        User user = new User();
        user.setName("键鹏");
        user.setGender("男");
        user.setPassword("root");
        user.setAge(19);
        user.setTel("18000110011");
        userMapper.insert(user);
    }
    /**
     * 删除
     */
    @RequestMapping("delete")
    @ResponseBody
    public void testRemove(){

        userMapper.deleteById(1480751909521403906L);
    }
    /**
     * 更新
     */
    @RequestMapping("update")
    @ResponseBody
    public void testUpdate(){
        User user =new User();
        user.setId(7L);
        user.setName("张小炮");  //注意： 生成update语句设置的字段为非空字段。
        userMapper.updateById(user); //update user set xx=xx ,xxx=xx ,xx=xx where id =xx
    }
    /**
     * 条件查询
     */
    @RequestMapping("find")
    @ResponseBody
    public void testFindByCondition(){
        //QueryWrapper代表就是条件
        QueryWrapper<User> queryWrapper  = new QueryWrapper<>();
        //添加条件
        queryWrapper.gt("age",18);
        List<User> userList = userMapper.selectList(queryWrapper);
        System.out.println("用户列表："+ userList);
    }
}
